using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Rensoft.CRM.Client.HostingAPI;
using Rensoft.CRM.Client.AccountsAPI;

namespace Rensoft.CRM.Client.Editors
{
    public partial class WebsiteEditor : BaseEditor
    {
        private WebsiteType selectedType;
        private WebsiteService websiteService;
        private Account account;

        public WebsiteEditor(Program program) : base(program)
        {
            InitializeComponent();
            InitializeTypeComboBox();

            SetMode(EditorMode.Create);
            Text = "New Website - " + AssemblyInfo.Title;
        }

        public WebsiteEditor(Program program, WebsiteService websiteService) : this(program)
        {
            SetMode(EditorMode.Modify);
            Text = websiteService.Title + " - " + AssemblyInfo.Title;

            this.websiteService = websiteService;
            titleTextBox.Text = websiteService.Title;
        }

        protected override void PerformClear()
        {
            base.PerformClear();

            titleTextBox.ResetText();
            typeComboBox.SelectedIndex = 0;
        }

        protected override void PerformLoad(DoWorkEventArgs e)
        {
            base.PerformLoad(e);

            InitializeAccountComboBox(clientComboBox, AccountType.Client);
            //HostingService hostingService = HostingService.CreateInstance();
            //MethodInvoker testMethod = new MethodInvoker(hostingService.Test);

            //if (!Program.ConfirmConnection(hostingService, testMethod))
            //{
            //    e.Cancel = true;
            //    return;
            //}

            switch (Mode)
            {
                case EditorMode.Modify:
                    {
                        // Website may have been removed outside of this application.
                        //if (!hostingService.WebsiteCheckGuidExists(websiteService.WebsiteGuid))
                        //{
                        //    MessageBox.Show(
                        //        "Could not find the hosting environment for this website.",
                        //        "Hosting Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //    e.Cancel = true;
                        //    return;
                        //}

                        //Website website = hostingService.WebsiteFetchByGuid(websiteService.WebsiteGuid);
                        //typeComboBox.SelectedValue = website.Types;
                    }
                    break;
            }

            base.PerformLoad(e);
        }

        protected override void OnLoadComplete(object sender, EventArgs e)
        {
            if (Mode == EditorMode.Modify)
            {
                foreach (AccountComboItem item in clientComboBox.Items)
                {
                    if (item.Value.AccountId == websiteService.AccountId)
                    {
                        clientComboBox.SelectedItem = item;
                    }
                }
            }

            base.OnLoadComplete(sender, e);
        }

        protected override void PerformSave(DoWorkEventArgs e)
        {
            AccountsService accountsService = AccountsService.CreateInstance();
            //HostingService hostingService = HostingService.CreateInstance();

            //MethodInvoker hostingTest = new MethodInvoker(hostingService.Test);
            //if (!Program.ConfirmConnection(hostingService, hostingTest))
            //{
            //    e.Cancel = true;
            //    return;
            //}

            string[] domainArray = new string[] {
                titleTextBox.Text,
                "www." + titleTextBox.Text
            };

            switch (Mode)
            {
                case EditorMode.Create:
                    {
                        //string[] usageList = hostingService.WebsiteDomainUsage(domainArray);
                        //if (usageList.Length != 0)
                        //{
                        //    MessageBox.Show(
                        //        "The domain '" + usageList[0] + "' is already in use.",
                        //        "Domain Usage", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //    e.Cancel = true;
                        //    return;
                        //}

                        //WebsiteService website = accountsService.WebsiteCreate(
                        //    titleTextBox.Text, account.AccountId);

                        //hostingService.WebsiteCreate(
                        //    website.WebsiteGuid,
                        //    account.AccountGuid,
                        //    titleTextBox.Text,
                        //    domainArray,
                        //    selectedType);
                    }
                    break;
            }
        }

        protected void InitializeTypeComboBox()
        {
            List<WebsiteTypeComboItem> itemList = new List<WebsiteTypeComboItem>();

            WebsiteTypeComboItem webAndEmail = new WebsiteTypeComboItem(
                "Web & Email", WebsiteType.Web | WebsiteType.Email);
            itemList.Add(webAndEmail);

            itemList.Add(new WebsiteTypeComboItem("Web Only", WebsiteType.Web));
            itemList.Add(new WebsiteTypeComboItem("Email Only", WebsiteType.Email));
            itemList.Add(new WebsiteTypeComboItem("Redirect", WebsiteType.Redirect));

            typeComboBox.DataSource = itemList;
            updateSelectedType();
        }

        private void typeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateSelectedType();
        }

        private void updateSelectedType()
        {
            if (typeComboBox.SelectedItem != null)
            {
                WebsiteTypeComboItem item = (WebsiteTypeComboItem)typeComboBox.SelectedItem;
                selectedType = item.Value;
            }
        }

        private void clientComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            AccountComboItem item = (AccountComboItem)clientComboBox.SelectedItem;
            account = item.Value;

            //HostingService hostingService = HostingService.CreateInstance();
            //bool guidExists = hostingService.ClientCheckGuidExists(account.AccountGuid);
            //settingsGroupBox.Enabled = guidExists;
            //EditorToolStrip.Enabled = guidExists;
        }
    }

    internal class WebsiteTypeComboItem
    {
        private string title;
        private WebsiteType type;

        public string Display
        {
            get { return title; }
        }

        public WebsiteType Value
        {
            get { return type; }
        }

        public WebsiteTypeComboItem(string title, WebsiteType type)
        {
            this.title = title;
            this.type = type;
        }
    }
}